<?php
class pagination extends CI_Controls{

	public $elementosPagina;

	public $elementosTotales;
	
	private $pag;

	/**
	 * numero de paginas a mostrar a la vez, ejem. 6 : (2 3 4 5 6 7)
	 * @var Integer
	 */
	public $numeroPaginas;

	public $url;

	public function  __construct(){
		parent::__construct();
		if(strstr($this->CI->uri->uri_string(),'pag')){
			$segment=array_keys($this->CI->uri->segment_array(),'pag');
			$this->pag=$this->CI->uri->segment($segment[0]+1);
		} else {
			$this->pag=1;
		}

	}
	public function drawContent(){

		if ($this->elementosPagina){
			$num_pags = ceil($this->elementosTotales/$this->elementosPagina);
		}
		
		if(strstr($this->CI->uri->uri_string(),'pag')){
			$i=0;
			$segments=$this->CI->uri->segment_array();
			$url='';
			$nseg=$this->CI->uri->total_segments();
			for (;$i<$nseg-2;$i++){
				$url.=$segments[$i+1].'/';
			}
			$url=$url;
			
		} else {
			$url=$this->CI->uri->uri_string().'/';
		}
		
		$flag='pag/';
		
		if ($this->elementosPagina && $num_pags>1){

			$cont_pag=new div();
			$cont_pag->atributos=$this->atributos;


			if($this->pag>1){

				$cont_ini=new div();
				$cont_ini->att('id',$this->att('id').'_cont_ini');
				$cont_ini->att('class',$this->att('class').'_CONT_INICIO');
				$cont_ini->value="<a href='".site_url($url.$flag.(1))."' class='".$this->att('class')."_INICIO'><<</a>";
				$cont_pag->addControl($cont_ini);
			} else {
				$this->CI->dynamic_meta->add_meta("<link rel='canonical' href='".site_url($url)."'>");
			}
			
			if($this->pag-1>=1){

				$cont_ant=new div();
				$cont_ant->att('id',$this->att('id').'_cont_ant');
				$cont_ant->att('class',$this->att('class').'_CONT_ANTERIOR');
				$cont_ant->value="<a rel='prev' href='".site_url($url.$flag.($this->pag-1))."' class='".$this->att('class')."_ANTERIOR'><</a>";
				$cont_pag->addControl($cont_ant);
				
				$this->CI->dynamic_meta->add_meta("<link rel='prev' href='".site_url($url.$flag.($this->pag-1))."'>");
			}

			$j=0;
			$paginasMostradas=$num_pags;
			if($this->numeroPaginas && $num_pags>$this->numeroPaginas){
				if($this->pag>$this->numeroPaginas/2){

					$j=$this->pag-(ceil($this->numeroPaginas/2)+1);

				}
				$paginasMostradas=$j+$this->numeroPaginas;
				if ($paginasMostradas>$num_pags){
					$j=$j-($paginasMostradas-$num_pags);
					$paginasMostradas=$num_pags;
				}
			}

			for(;$j<$paginasMostradas;$j++){
				$cont_pags=new div();
				$cont_pags->att('id',$this->att('id').'_cont_pag_'.$j);
				$cont_pags->att('class',$this->att('class').'_CONT_PAG');
				if($j+1==$this->pag){
					$cont_pags->value="<span class='".$this->att('class')."_PAGINA SELECTED'>".($j+1)."</span>";
				}
				else{
					$cont_pags->value="<a href='".site_url($url.$flag.($j+1))."' class='".$this->att('class')."_PAGINA'>".($j+1)."</a>";
				}
				$cont_pag->addControl($cont_pags);
			}

			if($this->pag+1<=$num_pags){
				$cont_sig=new div();
				$cont_sig->att('id',$this->att('id').'_cont_sig');
				$cont_sig->att('class',$this->att('class').'_CONT_SIGUIENTE');
				$cont_sig->value="<a rel='next' href='".site_url($url.$flag.($this->pag+1))."' class='".$this->att('class')."' '_SIGUIENTE'>></a>";
				$cont_pag->addControl($cont_sig);

				$this->CI->dynamic_meta->add_meta("<link rel='next' href='".site_url($url.$flag.($this->pag+1))."'>");
				
			}

			if($this->pag!=$num_pags){
				$cont_fin=new div();
				$cont_fin->att('id',$this->att('id').'_cont_fin');
				$cont_fin->att('class',$this->att('class').'_CONT_FINAL');
				$cont_fin->value="<a href='".site_url($url.$flag.($num_pags))."' class='".$this->att('class')."' '_FINAL'>>></a>";
				$cont_pag->addControl($cont_fin);
			}
			return $cont_pag;

		}




	}


	public  function get_elements_page(&$elementos){

		$ep=$this->elementosPagina;
		$et=count($elementos);
		if($ep){
			$num_pags = ceil($et/$ep);
			$e = ($this->pag-1) * $ep;
			$elementos = array_slice($elementos,$e,$ep);
		}
		return $elementos;

	}

	public  static function elements_pagination(&$elementos,$pagina,$ep){


		$et=count($elementos);
		$num_pags = ceil($et/$ep);
		$e = ($pagina-1) * $ep;
		$elementos = array_slice($elementos,$e,$ep);

		return $elementos;

	}

}